using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using DTO;
using DAO;
using System.Data;

namespace BUS
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class TVC_CATEControl
    {
        #region Local Variable
        private TVC_CATEDataAccess _objDAO;
        #endregion Local Variable

        #region Method
        public TVC_CATEControl()
        {
            _objDAO = new TVC_CATEDataAccess();
        }

        public TVC_CATEInfo Get(string db,
        String maingroup,
        String subgroup,
        String sizecode,
        String lengthcode,
        ref string sErr)
        {
            return _objDAO.Get(db,
            maingroup,
            subgroup,
            sizecode,
            lengthcode,
            ref sErr);
        }

        public DataTable GetAll(string db,
        ref string sErr)
        {
            return _objDAO.GetAll(db, ref sErr);
        }

        public Int32 Add(TVC_CATEInfo obj, ref string sErr)
        {
            return _objDAO.Add(obj, ref sErr);
        }

        public string Update(TVC_CATEInfo obj)
        {
            return _objDAO.Update(obj);
        }

        public string Delete(string db,
        String maingroup,
        String subgroup = null,
        String sizecode = null,
        String lengthcode = null
        )
        {
            return _objDAO.Delete(db,
            maingroup,
            subgroup,
            sizecode,
            lengthcode
            );
        }

        public Boolean IsExist(string db,
        String maingroup,
        String subgroup,
        String sizecode,
        String lengthcode
        )
        {
            return _objDAO.IsExist(db,
            maingroup,
            subgroup,
            sizecode,
            lengthcode
            );
        }

        public string InsertUpdate(TVC_CATEInfo obj)
        {
            string sErr = "";
            if (IsExist(obj.db,
            obj.maingroup,
            obj.subgroup,
            obj.sizecode,
            obj.lengthcode
            ))
            {
                sErr = Update(obj);
            }
            else
                Add(obj, ref sErr);
            return sErr;
        }

        public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            return _objDAO.GetTransferOut(dtb, from, to, ref sErr);
        }

        public DataTable ToTransferInStruct()
        {
            return TVC_CATEInfo.ToDataTable();
        }

        public string TransferIn(string db, DataRow row)
        {
            TVC_CATEInfo inf = new TVC_CATEInfo(row);
            inf.db = db;
            return InsertUpdate(inf);
        }
        #endregion Method


        public DataTable GetbyFilter(string db, string strFilter, ref string sErr)
        {
            return _objDAO.GetbyFilter(db, strFilter, ref sErr);
        }
        public object BeginTransaction(ref string sErr)
        {
            return _objDAO.BeginTransaction(ref sErr);
        }

        public bool CommitTransaction(ref string sErr)
        {
            return _objDAO.CommitTransaction(ref sErr);
        }

        public bool RollbackTransaction(ref string sErr)
        {
            return _objDAO.RollbackTransaction(ref sErr);
        }

        public bool SetTransaction(object trans)
        {
            return _objDAO.SetTransaction(trans);
        }

        public DataTable GetCateFrom12(string db, string maingroup, ref string sErr)
        {
            BUS.TVC_CATE1Control ctr = new TVC_CATE1Control();
            ctr.SetTransaction(this._objDAO.GetTransaction());
            DataTable dt = ctr.GetAll(db, maingroup, ref sErr);

            DataTable result = DTO.TVC_CATEInfo.ToDataTable();

            var arrmain = dt.AsEnumerable()
                .GroupBy(r => new { maingroup = r["maingroup"], mainname = r["mainname"] })
                .Select(g => new { maingroup = g.Key.maingroup, mainname = g.Key.mainname })
                .ToList();

            var arrsub = dt.AsEnumerable()
                .Where(r => r.Field<string>("subtype") == "1")
                .GroupBy(r => new { maingroup = r["maingroup"], subgroup = r["typevalue"], subname = r["typename"] })
                .Select(g => new { maingroup = g.Key.maingroup, subgroup = g.Key.subgroup, subname = g.Key.subname })
                .OrderBy(g => g.subgroup)
                .ToList();

            var arrsize = dt.AsEnumerable()
                .Where(r => r.Field<string>("subtype") == "2")
                .GroupBy(r => new { maingroup = r["maingroup"], sizecode = r["typevalue"], sizename = r["typename"] })
                .Select(g => new { maingroup = g.Key.maingroup, sizecode = g.Key.sizecode, sizename = g.Key.sizename })
                .OrderBy(g => g.sizecode)
                .ToList();

            var arrlen = dt.AsEnumerable()
                .Where(r => r.Field<string>("subtype") == "3")
                .GroupBy(r => new { maingroup = r["maingroup"], lengthcode = r["typevalue"], lengthname = r["typename"] })
                .Select(g => new { maingroup = g.Key.maingroup, lengthcode = g.Key.lengthcode, lengthname = g.Key.lengthname })
                .OrderBy(g => g.lengthcode)
                .ToList();

            int maxRowCount = Math.Max(arrsub.Count, arrsize.Count);
            maxRowCount = Math.Max(maxRowCount, arrlen.Count);
            for (int i = 0; i < maxRowCount; i++)
            {
                DataRow newR = result.NewRow();
                newR["maingroup"] = arrmain[0].maingroup;
                newR["mainname"] = arrmain[0].mainname;
                if (i < arrsub.Count)
                {
                    newR["subgroup"] = arrsub[i].subgroup;
                    newR["subname"] = arrsub[i].subname;
                }
                else
                {
                    newR["subgroup"] = "";
                    newR["subname"] = "";
                }
                if (i < arrsize.Count)
                {
                    newR["sizecode"] = arrsize[i].sizecode;
                    newR["sizename"] = arrsize[i].sizename;
                }
                else
                {
                    newR["sizecode"] = "";
                    newR["sizename"] = "";
                }
                if (i < arrlen.Count)
                {
                    newR["lengthcode"] = arrlen[i].lengthcode;
                    newR["lengthname"] = arrlen[i].lengthname;
                }
                else
                {
                    newR["lengthcode"] = "";
                    newR["lengthname"] = "";
                }
                result.Rows.Add(newR);
            }
            return result;
        }
    }
}
